//层
var HelloWorldLayer = cc.Layer.extend({
    sprite_bg:null,
    sprite_lanzi:null,
    arr_objArr:null,
    num_catchCount:0,
    num_gameLevel:0,
    _state : 1,
    _objBatch:null,
    _objControlCount:0,
    _objBadControlCount:0,
    _winSize:null,
    _lbScore:null,
    ctor:function () {
        this._super();
        this._winSize = cc.winSize;
        this.arr_objArr = [];

        //背景
        this.sprite_bg = new cc.Sprite("#bg.png");
        this.sprite_bg.attr({
            x: this._winSize.width / 2,
            y: this._winSize.height / 2,
            scale:1.2
        });
        this.addChild(this.sprite_bg, 0);

        //设置分数文字框
        this._lbScore = new cc.LabelTTF("0", "Arial", 40);
        this._lbScore.enableShadow(cc.color(0, 0, 0),5,5);
        this._lbScore.attr({
            x : this._winSize.width / 2,
            y : this._winSize.height - 50,
            color : cc.color(255, 255, 255)
        });
        this.addChild(this._lbScore, 1000);

        //篮子
        this.sprite_lanzi = new lanziSprite("#lanzi1.png");
        this.sprite_lanzi.attr({
            scale: 1,
            x: this._winSize.width / 2,
            y: this.sprite_lanzi.height/2+20
        });
        this.addChild(this.sprite_lanzi, 1);

        //创建篮子和水果的BatchNode
        var texTransparent = cc.textureCache.addImage(res.imgs_png);
        this._objBatch = new cc.SpriteBatchNode(texTransparent);
        this.addChild(this._objBatch,2);

        //触摸监听
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: this.onTouchBegan,
            onTouchMoved: this.onTouchMoved,
            onTouchEnded: this.onTouchEnded
        }, this);

        //开起动画
        this.scheduleUpdate();
        cc.audioEngine.playMusic(res.bgm_mp3, true);
    },
    onTouchBegan:function(touch){
        var pos = touch.getLocation();
        return true;
    },
    onTouchMoved:function(touch,event){
        var pos = touch.getLocation();
        event.getCurrentTarget().sprite_lanzi.attr({
            x:pos.x
        });
    },
    onTouchEnded:function(touch){
        var pos = touch.getLocation();
        return true;
    },
    update:function (dt) {
        this.objControl(dt);
        this.objUpdate(dt);
        this.checkIsCollide();
        this.updateUI();
    },
    updateUI:function () {
        this._lbScore.setString("总分: " + this.num_catchCount);
    },
    checkIsCollide:function(){
        var selChild,locShip = this.sprite_lanzi;
        //监测碰撞
        for (var i = 0; i < this.arr_objArr.length; i++) {
            selChild = this.arr_objArr[i];
            if (!selChild.active) {
                continue;
            }
            if (this.collide(selChild, locShip)) {
                //物品碰撞
                if(selChild.type == 0){
                    selChild.destroy();
                    this.num_catchCount += selChild.num;
                    this.creatPointFly(selChild.x,locShip.y,selChild.num);
                    //等级监听
                    if(this.num_catchCount > 240 && this.num_gameLevel != 5){
                        this.setGameLevel(5);
                    }else if(this.num_catchCount > 120 && this.num_gameLevel < 4){
                        this.setGameLevel(4);
                    }else if(this.num_catchCount > 60 && this.num_gameLevel < 3){
                        this.setGameLevel(3);
                    }else if(this.num_catchCount > 30 && this.num_gameLevel < 2){
                        this.setGameLevel(2);
                    }else if(this.num_catchCount > 10 && this.num_gameLevel < 1){
                        this.setGameLevel(1);
                    }
                }else if(selChild.type == 1){
                    selChild.destroy();
                    this.GameOver();
                }
            }
        }
    },
    creatPointFly:function(starX,starY,objNum){
        var thisObj = new objSprite("#obj"+objNum+".png");
            thisObj.attr({
                scale: 0.5,
                x: starX,
                y: starY,
                type:0
            });
            thisObj.runAction(
                cc.repeatForever(
                    cc.spawn(
                        cc.fadeOut(1.0),
                        cc.rotateTo(0.5,270),
                        cc.moveTo(1,cc.p(this._winSize.width/2,1000)).easing(cc.easeOut(2)),
                        cc.sequence(
                            cc.delayTime(1),
                            cc.callFunc(this.deletePointFly,this)
                        )
                    )
                )
            );
        this._objBatch.addChild(thisObj, 10, 5);
    },
    deletePointFly:function(obj){
        this._objBatch.removeChild(obj);
    },
    collide:function (a, b) {
        var ax = a.x;
        var ay = a.y;
        var bx = b.x;
        var by = b.y;
        if (Math.abs(ax - bx) > 70 || Math.abs(ay - by) > 70)
            return false;

        var aRect = a.collideRect(ax, ay);
        var bRect = b.collideRect(bx, by);
        return cc.rectIntersectsRect(aRect, bRect);
    },
    GameOver :function(){
        cc.log("onGameOver");
        cc.audioEngine.stopMusic();
        Zongfen = this.num_catchCount;
        cc.director.runScene(new cc.TransitionFade(0.2, new GameOverScene()));
    },
    setGameLevel:function(gl){
        //升级时要做的事情
        this.num_gameLevel = gl;
        var thisObj = new objSprite("#boom.png");
        thisObj.attr({
            scale: 0.5,
            x : this._winSize.width / 2 - thisObj.width / 2 * 2.5 + thisObj.width / 2 * this.num_gameLevel,
            y : this._winSize.height - 110,
            type:1
        });
        this._objBatch.addChild(thisObj, 10, 5);
    },
    objControl:function(){
        this._objControlCount++;
        this._objBadControlCount++;
        var spend;
        switch (this.num_gameLevel){
            case 0:
                spend = 40;
                break;
            case 1:
                spend = 25;
                break;
            case 2:
                spend = 15;
                break;
            case 3:
                spend = 10;
                break;
            case 4:
                spend = 5;
                break;
            case 5:
                spend = 2;
                break;
        }
        if(this._objControlCount >= spend){
            //添加下落物体
            var _random = parseInt(Math.random()*4 + 1);
            var thisObj = new objSprite("#obj"+_random+".png");
                thisObj.attr({
                    scale: 1,
                    x: parseInt(Math.random()*this._winSize.width),
                    y: this._winSize.height + 100,
                    num:_random
                });
                thisObj.runAction(
                    cc.repeatForever(
                        cc.spawn(
                            cc.rotateTo(2,270)
                        )
                    )
                );
            this._objBatch.addChild(thisObj, 1, 5);
            this.arr_objArr.push(thisObj);

            this._objControlCount = 0;
        }
        if(this._objBadControlCount >= 100) {
            //添加炸弹
            for(var i=0;i<this.num_gameLevel;i++){
                var _randomHight = i * parseInt(Math.random()*100 + 100);
                var thisObj = new objSprite("#boom.png");
                    thisObj.attr({
                        scale: 1,
                        x: parseInt(Math.random()*this._winSize.width),
                        y: this._winSize.height + 100 + _randomHight,
                        type:1
                    });
                    thisObj.runAction(
                        cc.repeatForever(
                            cc.spawn(
                                cc.rotateTo(2,270)
                            )
                        )
                    );
                this._objBatch.addChild(thisObj, 1, 5);
                this.arr_objArr.push(thisObj);
            }
            this._objBadControlCount = 0;
        }
    },
    objUpdate:function(dt){
        var selChild;
        if (this._state == 1) {
            for (var i in this.arr_objArr) {
                selChild = this.arr_objArr[i];
                selChild.update(dt);
            }
        }
    }
});

//物品
var objSprite = cc.Sprite.extend({
    yVelocity : 100,
    yVelocityJia : 30,
    active : true,
    num:1,
    type:0,
    ctor:function (aTexture) {
        this._super(aTexture);
    },
    update:function (dt) {
        var y = this.y;
        this.y = y - this.yVelocity * dt;
        this.yVelocity += this.yVelocityJia;

        if(this.y < -100){
            this.destroy();
        }
    },
    destroy:function () {
        //console.log("消除物品");
        this.yVelocity = 0;
        this.yVelocityJia = 0;
        this.y = 1000;
        this.visible = false;
        this.active = false;
        this.stopAllActions();
    },
    collideRect:function (x, y) {
        var w = this.width, h = this.height;
        return cc.rect(x - w / 2, y - h / 2, w, h);
    }
});

//篮子
var lanziSprite = cc.Sprite.extend({
    ctor:function (aTexture) {
        this._super(aTexture);

        //设置篮子动画
        var frame0 = cc.spriteFrameCache.getSpriteFrame("lanzi1.png");
        var frame1 = cc.spriteFrameCache.getSpriteFrame("lanzi2.png");

        var animFrames = [];
        animFrames.push(frame0);
        animFrames.push(frame1);

        var animation = new cc.Animation(animFrames, 0.1);
        var animate = cc.animate(animation);
        var action = animate.repeatForever();
        this.runAction(action);
    },
    collideRect:function (x, y) {
        var w = this.width, h = this.height;
        return cc.rect(x - w / 2, y - h / 2, w, h / 2);
    }
});

//游戏开始场景
var enterScene = cc.Scene.extend({
    layer:null,
    onEnter:function () {
        this._super();

        //预加载plist
        cc.spriteFrameCache.addSpriteFrames(res.imgs_plist);

        this.layer = new enterldLayer();
        this.addChild(this.layer,0);
    }
});

//游戏进行场景
var HelloWorldScene = cc.Scene.extend({
    layer:null,
    onEnter:function () {
        this._super();

        //预加载plist
        cc.spriteFrameCache.addSpriteFrames(res.imgs_plist);

        this.layer = new HelloWorldLayer();
        this.addChild(this.layer,0);
    }
});

//游戏结束场景
var GameOverScene  = cc.Scene.extend({
    onEnter:function () {
        this._super();

        var layer = new GameOverLayer();
        this.addChild(layer,1);
    }
});

//游戏开始层
var enterldLayer = cc.Layer.extend({
    _winSize:null,
    btn_play:null,
    sprite_bg:null,
    ctor : function(){
        this._super();
        this._winSize = cc.winSize;

        //背景
        this.sprite_bg = new cc.Sprite("#bg.png");
        this.sprite_bg.attr({
            x: this._winSize.width / 2,
            y: this._winSize.height / 2,
            scale:1
        });
        this.addChild(this.sprite_bg, 0);

        //标题大图
        var thisTitlePic = new objSprite(res.beginLOGO_png);
        thisTitlePic.attr({
            x:this._winSize.width / 2,
            y:this._winSize.height / 2 + 340,
            scale:0.8
        });
        this.addChild(thisTitlePic,10,5);

        //按钮背景
        var btnBG1 = new objSprite(res.bgBTN_png);
        btnBG1.attr({
            x:this._winSize.width / 4,
            y:80
        });
        var btnBG2 = new objSprite(res.bgBTN_png);
        btnBG2.attr({
            x:this._winSize.width / 4 * 3,
            y:80
        });
        this.addChild(btnBG1);
        this.addChild(btnBG2);
        this.getGameInfo();
    },
    getGameInfo:function(){
        var thisInfo;
        var that = this;
        var xhr = cc.loader.getXMLHttpRequest();
        var args = "openid="+userData.openid+"&id="+userData.id;

        // xhr.open("POST", "/MicroRankGameTel/GetGameStatus");
        // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset\x3dUTF-8");
        // xhr.send(args);
        // xhr.onreadystatechange = function () {
        //     if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
        //         thisInfo = JSON.parse(xhr.responseText);
        //         that.setUI(thisInfo);
        //     }
        // };

        thisInfo = {
           //0未开始 1进行中 2已结束
           status:1,
           beginDate: "2015/10/29 13:54",
           score:934
        }
        this.setUI(thisInfo);
    },
    setUI:function(resObj){
        //判断游戏状态
        if(resObj.status == 0){
            //文字按钮
            cc.MenuItemFont.setFontName("幼圆");
            cc.MenuItemFont.setFontSize(30);
            var textBtn = new cc.MenuItemFont("活动说明",function(){
                indexDesc();
            },this);
            var textBtn2 = new cc.MenuItemFont("排行榜",function(){
                indexRank();
            },this);
            var MIF = new cc.Menu(textBtn);
            MIF.attr({
                x:this._winSize.width / 4,
                y:80
            });
            var MIF2 = new cc.Menu(textBtn2);
            MIF2.attr({
                x:this._winSize.width / 4 * 3,
                y:80
            });
            this.addChild(MIF);
            this.addChild(MIF2);

            //篮子
            this.sprite_lanzi = new lanziSprite("#lanzi1.png");
            this.sprite_lanzi.attr({
                scale: 1,
                x:this._winSize.width / 2,
                y:this._winSize.height / 2 - 170,
            });
            this.addChild(this.sprite_lanzi, 1);

            //添加文字
            var lbScoreStar = new cc.LabelTTF("等你哟~", "幼圆", 30);
            lbScoreStar.enableShadow(cc.color(0, 0, 0),5,5);
            lbScoreStar.x = this._winSize.width / 2 + 15;
            lbScoreStar.y = this._winSize.height / 2 - 260;
            lbScoreStar.color = cc.color(255, 255, 255);
            lbScoreStar.textAlign = cc.TEXT_ALIGNMENT_CENTER;
            this.addChild(lbScoreStar,1);

            var btnBG2 = new objSprite(res.bgBTN_png);
                btnBG2.attr({
                    x:this._winSize.width / 2 + 15,
                    y:this._winSize.height / 2 + 40,
                    scale: 2.5
                });
            this.addChild(btnBG2);

            var lbScoreStar1 = new cc.LabelTTF("活动未开始！", "幼圆", 35);
            lbScoreStar1.enableShadow(cc.color(0, 0, 0),5,5);
            lbScoreStar1.x = this._winSize.width / 2 + 15;
            lbScoreStar1.y = this._winSize.height / 2 + 40;
            lbScoreStar1.color = cc.color(255, 255, 255);
            lbScoreStar1.textAlign = cc.TEXT_ALIGNMENT_CENTER;
            this.addChild(lbScoreStar1,1);

            var lbScoreStar2 = new cc.LabelTTF("开始时间："+resObj.beginDate, "幼圆", 30);
            lbScoreStar2.enableShadow(cc.color(0, 0, 0),5,5);
            lbScoreStar2.x = this._winSize.width / 2 + 15;
            lbScoreStar2.y = this._winSize.height / 2 + 180;
            lbScoreStar2.color = cc.color(255, 255, 255);
            lbScoreStar2.textAlign = cc.TEXT_ALIGNMENT_CENTER;
            this.addChild(lbScoreStar2,1);

        }else if(resObj.status == 2){
            //文字按钮
            cc.MenuItemFont.setFontName("幼圆");
            cc.MenuItemFont.setFontSize(30);
            var textBtn = new cc.MenuItemFont("活动说明",function(){
                indexDesc();
            },this);
            var textBtn2 = new cc.MenuItemFont("排行榜",function(){
                indexRank();
            },this);
            var textBtn3 = new cc.MenuItemFont("查看我的中奖",function(){
                indexRes();
            },this);
            var MIF = new cc.Menu(textBtn);
            MIF.attr({
                x:this._winSize.width / 4,
                y:80
            });
            var MIF2 = new cc.Menu(textBtn2);
            MIF2.attr({
                x:this._winSize.width / 4 * 3,
                y:80
            });
            var MIF3 = new cc.Menu(textBtn3);
            MIF3.attr({
                x:this._winSize.width / 2,
                y:this._winSize.height / 2 - 260
            });
            this.addChild(MIF);
            this.addChild(MIF2);
            this.addChild(MIF3);

            //篮子
            this.sprite_lanzi = new lanziSprite("#lanzi1.png");
            this.sprite_lanzi.attr({
                scale: 1,
                x:this._winSize.width / 2,
                y:this._winSize.height / 2 - 170,
            });
            this.addChild(this.sprite_lanzi, 1);

            //添加文字
            var btnBG2 = new objSprite(res.bgBTN_png);
            btnBG2.attr({
                x:this._winSize.width / 2 + 15,
                y:this._winSize.height / 2 + 40,
                scale: 2.5
            });
            this.addChild(btnBG2);

            var lbScoreStar1 = new cc.LabelTTF("活动已结束！", "幼圆", 35);
            lbScoreStar1.enableShadow(cc.color(0, 0, 0),5,5);
            lbScoreStar1.x = this._winSize.width / 2 + 15;
            lbScoreStar1.y = this._winSize.height / 2 + 40;
            lbScoreStar1.color = cc.color(255, 255, 255);
            lbScoreStar1.textAlign = cc.TEXT_ALIGNMENT_CENTER;
            this.addChild(lbScoreStar1,1);

            var lbScoreStar2 = new cc.LabelTTF("最终得分："+resObj.score, "幼圆", 45);
            lbScoreStar2.enableShadow(cc.color(0, 0, 0),5,5);
            lbScoreStar2.x = this._winSize.width / 2 + 15;
            lbScoreStar2.y = this._winSize.height / 2 + 180;
            lbScoreStar2.color = cc.color(255, 255, 255);
            lbScoreStar2.textAlign = cc.TEXT_ALIGNMENT_CENTER;
            this.addChild(lbScoreStar2,1);

        }else{
            //篮子
            var playAgainNormal = new lanziSprite("#lanzi1.png");
            playAgainNormal.attr({
                scale:1
            });
            var playAgainSelected = new lanziSprite("#lanzi1.png");
            playAgainSelected.attr({
                scale:1.05
            });
            var playAgainDisabled = new lanziSprite("#lanzi1.png");
            playAgainDisabled.attr({
                scale:1.05
            });

            var playAgain = new cc.MenuItemSprite(
                playAgainNormal,
                playAgainSelected,
                playAgainDisabled,
                this.onPlayAgain,this);

            this.btn_boom = new cc.Menu(playAgain);
            this.btn_boom.attr({
                x:this._winSize.width / 2,
                y:this._winSize.height / 2 - 170,
                scale:1
            });
            this.addChild(this.btn_boom,10);

            //文字按钮
            cc.MenuItemFont.setFontName("幼圆");
            cc.MenuItemFont.setFontSize(30);
            var textBtn = new cc.MenuItemFont("活动说明",function(){
                indexDesc();
            },this);
            var textBtn2 = new cc.MenuItemFont("排行榜",function(){
                indexRank();
            },this);
            var MIF = new cc.Menu(textBtn);
            MIF.attr({
                x:this._winSize.width / 4,
                y:80
            });
            var MIF2 = new cc.Menu(textBtn2);
            MIF2.attr({
                x:this._winSize.width / 4 * 3,
                y:80
            });
            this.addChild(MIF);
            this.addChild(MIF2);

            //说明
            var thisObj1 = new objSprite("#obj1.png");
            thisObj1.attr({
                x:this._winSize.width / 2 - 180,
                y:this._winSize.height / 2 + 180,
                scale:0.8
            });
            var thisObj2 = new objSprite("#obj2.png");
            thisObj2.attr({
                x:this._winSize.width / 2 - 180,
                y:this._winSize.height / 2 + 70,
                scale:0.8
            });
            var thisObj3 = new objSprite("#obj3.png");
            thisObj3.attr({
                x:this._winSize.width / 2 - 180,
                y:this._winSize.height / 2 - 40,
                scale:0.8
            });
            var thisObj4 = new objSprite("#obj4.png");
            thisObj4.attr({
                x:this._winSize.width / 2 + 40,
                y:this._winSize.height / 2 + 180,
                scale:0.8
            });
            var thisObj5 = new objSprite("#obj5.png");
            thisObj5.attr({
                x:this._winSize.width / 2 + 40,
                y:this._winSize.height / 2 + 70,
                scale:0.8
            });
            var thisObj6 = new objSprite("#boom.png");
            thisObj6.attr({
                x:this._winSize.width / 2 + 40,
                y:this._winSize.height / 2 - 40,
                scale:0.8
            });
            this.addChild(thisObj1, 10, 5);
            this.addChild(thisObj2, 10, 5);
            this.addChild(thisObj3, 10, 5);
            this.addChild(thisObj4, 10, 5);
            this.addChild(thisObj5, 10, 5);
            this.addChild(thisObj6, 10, 5);

            //添加文字
            var lbScoreStar = new cc.LabelTTF("点击开始挑战！", "幼圆", 30);
            lbScoreStar.enableShadow(cc.color(0, 0, 0),5,5);
            lbScoreStar.x = this._winSize.width / 2 + 15;
            lbScoreStar.y = this._winSize.height / 2 - 260;
            lbScoreStar.color = cc.color(255, 255, 255);
            lbScoreStar.textAlign = cc.TEXT_ALIGNMENT_CENTER;
            this.addChild(lbScoreStar,1);

            var lbScore1 = new cc.LabelTTF("x 1分", "幼圆", 30);
            var lbScore2 = new cc.LabelTTF("x 2分", "幼圆", 30);
            var lbScore3 = new cc.LabelTTF("x 3分", "幼圆", 30);
            var lbScore4 = new cc.LabelTTF("x 4分", "幼圆", 30);
            var lbScore5 = new cc.LabelTTF("x 5分", "幼圆", 30);
            var lbScore6 = new cc.LabelTTF("x 炸弹！", "幼圆", 35);
            lbScore1.enableShadow(cc.color(0, 0, 0),5,5);
            lbScore2.enableShadow(cc.color(0, 0, 0),5,5);
            lbScore3.enableShadow(cc.color(0, 0, 0),5,5);
            lbScore4.enableShadow(cc.color(0, 0, 0),5,5);
            lbScore5.enableShadow(cc.color(0, 0, 0),5,5);
            lbScore6.enableShadow(cc.color(0, 0, 0),5,5);
            lbScore1.x = this._winSize.width / 2 - 80;
            lbScore2.x = this._winSize.width / 2 - 80;
            lbScore3.x = this._winSize.width / 2 - 80;
            lbScore4.x = this._winSize.width / 2 + 140;
            lbScore5.x = this._winSize.width / 2 + 140;
            lbScore6.x = this._winSize.width / 2 + 170;
            lbScore1.y = this._winSize.height / 2 + 180;
            lbScore2.y = this._winSize.height / 2 + 70;
            lbScore3.y = this._winSize.height / 2 - 40;
            lbScore4.y = this._winSize.height / 2 + 180;
            lbScore5.y = this._winSize.height / 2 + 70;
            lbScore6.y = this._winSize.height / 2 - 40;
            lbScore1.color = cc.color(255, 255, 255);
            lbScore2.color = cc.color(255, 255, 255);
            lbScore3.color = cc.color(255, 255, 255);
            lbScore4.color = cc.color(255, 255, 255);
            lbScore5.color = cc.color(255, 255, 255);
            lbScore6.color = cc.color(255, 255, 255);
            lbScore1.textAlign = cc.TEXT_ALIGNMENT_LEFT;
            lbScore2.textAlign = cc.TEXT_ALIGNMENT_LEFT;
            lbScore3.textAlign = cc.TEXT_ALIGNMENT_LEFT;
            lbScore4.textAlign = cc.TEXT_ALIGNMENT_LEFT;
            lbScore5.textAlign = cc.TEXT_ALIGNMENT_LEFT;
            lbScore6.textAlign = cc.TEXT_ALIGNMENT_LEFT;
            this.addChild(lbScore1,1);
            this.addChild(lbScore2,1);
            this.addChild(lbScore3,1);
            this.addChild(lbScore4,1);
            this.addChild(lbScore5,1);
            this.addChild(lbScore6,1);
        }
    },
    onPlayAgain : function(){
        cc.director.runScene(new cc.TransitionFade(0.2, new HelloWorldScene()));
    }
});

//游戏结束层
var Zongfen = 0;
var GameOverLayer = cc.Layer.extend({
    btn_boom:null,
    sprite_bg:null,
    _winSize:null,
    ctor : function(){
        this._super();
        this._winSize = cc.winSize;
        this.initScore();
        this.initTouchRank();
        this.initTouchShare();

        //背景
        this.sprite_bg = new cc.Sprite("#bg.png");
        this.sprite_bg.attr({
            x: this._winSize.width / 2,
            y: this._winSize.height / 2
        });
        this.addChild(this.sprite_bg, 0);

        var playAgainNormal = new cc.Sprite("#boom.png");
            playAgainNormal.attr({
                scale:1
            });
        var playAgainSelected = new cc.Sprite("#boom.png");
            playAgainSelected.attr({
                scale:1.05
            });
        var playAgainDisabled = new cc.Sprite("#boom.png");
            playAgainDisabled.attr({
                scale:1.05
            });

        var playAgain = new cc.MenuItemSprite(
            playAgainNormal,
            playAgainSelected,
            playAgainDisabled,
            this.onPlayAgain,this);

        this.btn_boom = new cc.Menu(playAgain);
        this.btn_boom.attr({
            x:this._winSize.width,
            y:this._winSize.height + 240,
            scale:2
        });
        this.addChild(this.btn_boom,10);

        //保存分数
        this.SavePoint(Zongfen);
    },
    initScore : function(){
        var lbScore = new cc.LabelTTF("最终得分:"+ Zongfen, "Arial Bold", 60);
            lbScore.enableShadow(cc.color(0, 0, 0),5,5);
            lbScore.x = this._winSize.width / 2;
            lbScore.y = this._winSize.height / 2 + 400;
            lbScore.color = cc.color(255, 255, 255);
        this.addChild(lbScore,1);

        var lbScore2 = new cc.LabelTTF("点击再次挑战！", "Arial Bold", 35);
            lbScore2.enableShadow(cc.color(0, 0, 0),5,5);
            lbScore2.x = this._winSize.width / 2;
            lbScore2.y = this._winSize.height / 2 + 80;
            lbScore2.color = cc.color(255, 255, 255);
        this.addChild(lbScore2,1);
    },
    initTouchShare : function(){
        var playAgainNormal = new cc.Sprite("#btn1.png");
            playAgainNormal.attr({
                scale:1
            });
        var playAgainSelected = new cc.Sprite("#btn1.png");
            playAgainSelected.attr({
                scale:1.05
            });
        var playAgainDisabled = new cc.Sprite("#btn1.png");
            playAgainDisabled.attr({
                scale:1.05
            });

        var playAgain = new cc.MenuItemSprite(
            playAgainNormal,
            playAgainSelected,
            playAgainDisabled,
            this.onRank,this);

        var menu = new cc.Menu(playAgain);
            menu.x = this._winSize.width / 2;
            menu.y = this._winSize.height / 2 - 25;

        this.addChild(menu,1);
    },
    initTouchRank : function(){
        var playAgainNormal = new cc.Sprite("#btn2.png");
            playAgainNormal.attr({
                scale:1
            });
        var playAgainSelected = new cc.Sprite("#btn2.png");
            playAgainSelected.attr({
                scale:1.05
            });
        var playAgainDisabled = new cc.Sprite("#btn2.png");
            playAgainDisabled.attr({
                scale:1.05
            });

        var playAgain = new cc.MenuItemSprite(
            playAgainNormal,
            playAgainSelected,
            playAgainDisabled,
            this.onShare,this);

        var menu = new cc.Menu(playAgain);
        menu.x = this._winSize.width / 2;
        menu.y = this._winSize.height / 2 - 150;

        this.addChild(menu,1);
    },
    onPlayAgain : function(){
        cc.director.runScene(new cc.TransitionFade(0.2, new HelloWorldScene()));
    },
    onShare : function(){
        indexShare();
    },
    onRank : function(){
        indexRank();
    },
    SavePoint: function(zongfen){
        var that = this;
        var xhr = cc.loader.getXMLHttpRequest();

        // xhr.open("POST", "/MicroRankGameTel/AddRecord/"+userData.id);
        // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset\x3dUTF-8");
        // xhr.onreadystatechange = function () {
        //     if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
        //         var responseArr = JSON.parse(xhr.responseText)
        //         console.log(responseArr);
        //     }
        // };
        // var args = "openid="+userData.openid+"&pid="+userData.pid+"&score="+zongfen;
        // xhr.send(args);
    }
});
